package com.itboye.ihomebank.home;

import java.util.ArrayList;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.baidu.mapapi.map.Text;
import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.util.ByAlert;

public class ActivityEditFangZuXinXi extends BaseOtherActivity {
	ImageView close_icon;
	TextView add_lift_tv, save, txt_fenqishu;
	ToggleButton toggle_button;
	RelativeLayout xuanzefenqi, bufenqi;
	LinearLayout qishu_container;
	private RelativeLayout layout;
	private PopupWindow mPopWin;
	protected int totalFenQi;
	ArrayList<EditText> arrayListEdit = new ArrayList<>();
	boolean isExpand = true;
	int qishu;
	TextView txt;
	EditText edit;
	private ArrayAdapter adapter;
	String totalDate;
	RelativeLayout shifoutongyi;

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent intent = null;
		switch (v.getId()) {
		case R.id.close_icon:
			finish();
			break;
		case R.id.save:
			StringBuffer stringBuffer = new StringBuffer();
			boolean isEmpty = false;
			if (isExpand == false) {// 选择了 不统一分期

				for (int i = 0; i < arrayListEdit.size(); i++) {
					String edit = arrayListEdit.get(i).getText().toString();
					if (edit.equals("")) {
						isEmpty = true;
						break;
					} else {
						stringBuffer.append(edit + ",");
					}
				}

				if (isEmpty) {
					ByAlert.alert("请将租金信息输入完整");
					return;
				}
			} else {

				if (edit.getText().toString().equals("")) {
					ByAlert.alert("请输入每年应付的金额");
					return;
				}
				for (int i = 0; i < totalFenQi; i++) {
					stringBuffer.append(edit.getText().toString() + ",");
				}
			}
			System.out.println(stringBuffer.toString() + "________>");
			intent = new Intent();
			intent.putExtra("zujinxinxi",
					stringBuffer.length() > 0 ? stringBuffer.toString()
							.substring(0, stringBuffer.length() - 1) : "");
			setResult(106, intent);
			finish();
			break;
		case R.id.xuanzefenqi:
			// chuzuPopWindow(v);
			break;
		case R.id.toggle_button:

			if (isExpand) {
				if (totalFenQi == 0) {
					ByAlert.alert("请先选择分期");
					return;
				}
				bufenqi.setVisibility(View.GONE);
				fenqi(totalFenQi);
			} else {
				edit.setInputType(InputType.TYPE_CLASS_NUMBER);
				txt.setText("请输入每期租金:");
				edit.setHint("请输入每期租金");
				bufenqi.setVisibility(View.VISIBLE);
				qishu_container.removeAllViews();
				arrayListEdit.clear();
			}

			isExpand = !isExpand;
			break;
		default:
			break;
		}
	}

	private void fenqi(int totalFenQi) {
		// TODO Auto-generated method stub
		qishu_container.removeAllViews();
		for (int i = 0; i < totalFenQi; i++) {
			View view = LayoutInflater.from(this).inflate(R.layout.item_hetong,
					null);
			LayoutParams layoutParams = new LayoutParams(
					LayoutParams.MATCH_PARENT, 70);
			layoutParams.setMargins(0, 5, 0, 5);
			TextView textView = (TextView) view.findViewById(R.id.txt);
			textView.setText("第" + (i + 1) + "年月租金");

			EditText edt = (EditText) view.findViewById(R.id.edit);
			edt.setInputType(InputType.TYPE_CLASS_NUMBER);
			edt.setTextSize(16);
			arrayListEdit.add(edt);
			edt.setHint("输入第" + (i + 1) + "年月租金");
			qishu_container.addView(view, layoutParams);
		}
	}

	/**
	 * 出租方式
	 */
	// private void chuzuPopWindow(View v) {
	// layout = (RelativeLayout) LayoutInflater.from(
	// ActivityEditFangZuXinXi.this).inflate(R.layout.popup_fangyuan,
	// null);
	// ArrayList<Integer> data = new ArrayList<Integer>();
	// for (int i = 0; i < qishu; i++) {
	// data.add(i + 1);
	// }
	// ListView listView = (ListView) layout
	// .findViewById(R.id.fangyuan_listView);
	// android.widget.RelativeLayout.LayoutParams params = new
	// android.widget.RelativeLayout.LayoutParams(
	// android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,
	// android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
	// params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
	// listView.setLayoutParams(params);
	// adapter = new ArrayAdapter(this, R.layout.text, data);
	// listView.setAdapter(adapter);
	// mPopWin = new PopupWindow(layout, ViewGroup.LayoutParams.MATCH_PARENT,
	// ViewGroup.LayoutParams.MATCH_PARENT, true);
	// mPopWin.setBackgroundDrawable(new BitmapDrawable());
	// mPopWin.showAsDropDown(v, 0, 0);
	// mPopWin.update();
	//
	// listView.setOnItemClickListener(new OnItemClickListener() {
	//
	// @Override
	// public void onItemClick(AdapterView<?> parent, View view,
	// int position, long id) {
	// if (isExpand == false) {
	// if ((position + 1) == totalFenQi) {
	// qishu_container.removeAllViews();
	// }
	// txt_fenqishu.setText((position + 1) + "");
	// totalFenQi = position + 1;
	// if (totalFenQi > 0) {
	// toggle_button.setEnabled(true);
	// }
	// if (isExpand) {
	// fenqi(totalFenQi);
	// }
	// }
	//
	// mPopWin.dismiss();
	// }
	// });
	//
	// }

	@Override
	protected int layout_id() {
		// TODO Auto-generated method stub
		return R.layout.activity_hetong_fangzuxinxi;
	}

	@Override
	protected void onInitController() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onInit() {
		totalDate = getIntent().getStringExtra("totalDate");
		String[] tempDate = totalDate.split("-");
		String str = tempDate[0] + "年" + tempDate[1] + "月" + tempDate[2] + "天";
		if (!tempDate[0].equals("0") && !tempDate[1].equals("0")// 年月日
				&& !tempDate[2].equals("0")) {
			str =  tempDate[0] + "年" + tempDate[1] + "月" + tempDate[2]
					+ "天";
		} else if (tempDate[0].equals("0") && !tempDate[1].equals("0")// 月日
				&& !tempDate[2].equals("0")) {
			str =  tempDate[1] + "月" + tempDate[2] + "天";
		} else if (!tempDate[0].equals("0") && tempDate[1].equals("0")// 年日
				&& !tempDate[2].equals("0")) {
			str =  tempDate[0] + "年" + tempDate[2] + "天";
		} else if (tempDate[0].equals("0") && tempDate[1].equals("0")// 日
				&& !tempDate[2].equals("0")) {
			str =  tempDate[2] + "天";
		} else if (!tempDate[0].equals("0") && !tempDate[1].equals("0")// 年月
				&& tempDate[2].equals("0")) {
			str =  tempDate[0] + "年" + tempDate[1] + "月";
		} else if (tempDate[0].equals("0") && !tempDate[1].equals("0")// 月
				&& tempDate[2].equals("0")) {
			str =  tempDate[1] + "月";
		} else if (!tempDate[0].equals("0") && tempDate[1].equals("0")// 年
				&& tempDate[2].equals("0")) {
			str =  tempDate[0] + "年";
		}
		// else if (!tempDate[0].equals("0") && !tempDate[1].equals("0")// 年月
		// && tempDate[2].equals("0")) {
		// str = "租期：" + tempDate[0] + "年" + tempDate[1] + "月";
		// }
		// if (tempDate[0].equals("0")) {
		// shifoutongyi.setVisibility(View.GONE);
		// } else {
		// shifoutongyi.setVisibility(View.VISIBLE);
		// }
		txt_fenqishu.setText(str);
		edit.setInputType(InputType.TYPE_CLASS_NUMBER);
		txt.setText("请输入每年月租金:");
		edit.setHint("请输入每年月租金");
		save.setVisibility(View.VISIBLE);
		save.setTextSize(16);
		add_lift_tv.setText("填写房租信息");
		qishu = getIntent().getIntExtra("qishu", 0);
		if (qishu == 0) {
			qishu=1;
//			toggle_button.setEnabled(false);
		}
//		else {
//			toggle_button.setEnabled(true);
//		}
		// txt_fenqishu.setText(qishu + "");
		totalFenQi = qishu;
	}

	@Override
	protected void onReadIntent(Bundle bundle) {
		// TODO Auto-generated method stub

	}

}
